/**
 * Created by Administrator on 2016/12/14.
 */

$(function () {
    init();
    lookUp();


});
/*****初始化表格数据 *****************/
function init() {
    $("#pjTable").bootstrapTable({
        method: 'post',
        url:'http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition',
        onLoadSuccess:function(data){       //console.log(data);
             $('.bs-checkbox').hide();
            //console.log(data);

        },
        onClickCell:function(field, value, row, $element) {
            var str="";
            for(var key in row){
                str+="&"+key+"="+row[key];
            }
            if (field =="edit") {
                layer.open({
                    title: '项目管理/项目详情',
                    shadeClose: true,
                    shade: 0.8,
                    type: 2,
                    fix: false,
                    maxmin: true,
                    area: ['100%', '100%'],
                    content: `../projectBasicData/basicDetails/view/projectDetailsView/projectView.html?${str.slice(1)}`,
                    end:function(){
                        $("#pjTable").bootstrapTable("refresh",'http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition')
                    }
                })
            }
        },
        onPageChange:function(number,size){
        // console.log()  ;
            if($('.bs-checkbox').css('display')=="block"){
                $('.bs-checkbox').show();
            }
        }
    });
};
function responseHandler(res){
    //console.log(res);
    var needData = {
        rows: res.data.list,//只 输出data,内容不会显示 在页面内。必须 加上.list;
        total: res.data.total
    };
    return needData;
}
function processFmt(val,row,index){
    //console.log(val);
        return ((val==0)?'在建':'完工');
}
function editFmt(val,row,index){
    return " <button class='btn btn-info'>查看/编辑</button>";

}
function indexFmt(val,row,index){
    var optionVar=$("table#pjTable").data('bootstrap.table').options;

    var pageNum=optionVar.pageNumber;
    var pageSize=optionVar.pageSize;
   return (pageNum-1)*pageSize+index+1;
}

function pjTableQuery(params){
    params["pageNum"] = (params.offset / params.limit) + 1;
    params.pageSize = params.limit;
    return params;
}

///////////////开工日期格式化////////////////
function cTimeFmt(index,row,val){
     return  getMyTime(row.ctime);
}
/********搜索功能*************/
function lookUp(){
    $('.search-btn').click(function(){
        //console.log(this);
        var item =$(this).parent().find('[type="text"]');
        var obj="";
        item.each(function(i,e){
            obj+=$(e).val()+",";
        });
        obj=obj.split(",");
        $.ajax({
            type:"post",
            url:`http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition`,
            contentType:"application/json",
            dataType:"json",
            data:JSON.stringify({
                "projectCode":obj[0],
                "projectName":obj[1],
                "constructUnit":obj[2],
                "constructionUnit":obj[3]
            }),
            success:function(data){
                console.log(data);
                if(data.data==null){
                    $('#pjTable').bootstrapTable("removeAll");
                }else{
                    var  oSearch = responseHandler(data);
                    $('#pjTable').bootstrapTable("load",oSearch);
                    $('.bs-checkbox').hide();
                }
            },
            error:function(err){
                console.log(err);
            }
        })

    })
}
/*************清空搜索条件*****************/
$('.clear-btn').click(function(){
    var item =$(this).parent().find('[type="text"]');
    item.each(function(i,e){
        $(e).val("");
    })
    $('#pjTable').bootstrapTable("refresh",`http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition`)
})
/********点击新增项目用layer弹出新页面****************/
$('.addProject').click(function(){
    layer.open({
        title: "项目管理/新增项目",
        shadeClose: true,
        shade: 0.8,
        type: 2,
        fix: false,
        maxmin: true,
        area: ['100%', '100%'],
        content: '../projectBasicData/addDetails/addProject/addProject.html'
    })
});
/********点击批量操作显示表格的全选按钮**********/

$('.dealAll').click(function(){
   // console.log(this.innerHTML);
    if(this.innerHTML=="批量操作"){
         $('.bs-checkbox').show();
        $('.fixed-table-footer').append('\
            <button class="btn btn-primary over">完工</button>\
            <button class="btn btn-info print">打印</button>\
            <button class="btn btn-danger remove">删除</button>\
            <button class="btn btn-warning down">下载</button>\
            ');
        this.innerHTML="取消操作";
    }else{
        $('.bs-checkbox').hide();
        this.innerHTML="批量操作";
        $('.fixed-table-footer').html("");
    }
});
/***********点击完工改变选中项目的进程状态为完工，点击删除则删除所选中数据***************/
$('.fixed-table-footer').on("click","button",function(e){
    var pId=$('.selected td:eq(1)').text();console.log(pId);
    console.log(this.innerHTML);
    if(this.innerHTML=="完工"){//当点击 完工的时候提交数据
        console.log(111);


        //多个项目ID之间用逗号分隔
        $.ajax({
            type:'put',
            contentType:"application/json",
            url:`http://192.168.59.3:8082/console/projectInfo/batchUpdateProjectCourse?projectIds=${pId}`,
            success:function(data){
                console.log(data.data);
                $('.selected td:eq(8)').text("完工");

            },
            error:function(err){
                console.log(err);
            }
        });

    };
    if(this.innerHTML=="删除"){//当点击删除的时候提交数据到后删除数据
        console.log(222);
        $.ajax({
            type:'put',
            contentType:"application/json",
            url:`http://192.168.59.3:8082/console/projectInfo/batchDeleteProjects?projectIds=${pId}`,
            data:'{"projectIds":pId}',
            success:function(data){
                console.log(data);
            },
            error:function(err){
                console.log(err);
            }
        });
        $('.selected').remove();
    }
})
/********格式化日期*********/
function getMyTime(date){
    date=new Date(date);
    var Y=date.getFullYear();
    var M=date.getMonth();
    var D=date.getDate();
    return `${Y}/${M}/${D}`
}


/********搜索栏里项目进程状态 完工，在建 ********/
function changeState(){
    $('input[type="radio"]').click(function(){
       console.log($(this).val());
        if($(this).val()==1){
            $('#pjTable').bootstrapTable("refresh",'`http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition`')
        }else if($(this).val()==2){
            $.ajax({
                type:'post',
                url:'http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition',
                data:'{"projectCourse":0}',
                contentType:'application/json',
                success:function(data){
                    var oBuild=responseHandler(JSON.parse(data));
                    $('#pjTable').bootstrapTable("load",oBuild);
                    $('.bs-checkbox').hide();
                },
                error:function(err){
                    console.log(err);
                }
            })
        }else if($(this).val()==3){
            $.ajax({
                type:'post',
                url:'http://192.168.59.3:8082/console/projectInfo/findProjectAllByCondition',
                data:'{"projectCourse":1}',
                contentType:'application/json',
                success:function(data){
                  //  console.log(JSON.parse(data));
                    var oBuild=responseHandler(JSON.parse(data));
                    $('#pjTable').bootstrapTable("load",oBuild);
                       $('.bs-checkbox').hide();

                },
                error:function(err){
                    console.log(err);
                }

            })
        }
    })
}
changeState();